/*
* Copyright (C) 2008-2009 J-P Nurmi jpnurmi@gmail.com
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
* $Id: homeview.cpp 549 2009-05-15 04:46:40Z jpnurmi $
*/

#include "homeview.h"
#include "application.h"
#include <QDesktopServices>
#include <QVBoxLayout>
#include <QTabBar>
#include <QLabel>
#include <QDebug>
#include <QFile>
#include <QUrl>

HomeView::HomeView(QWidget* parent) : QTabWidget(parent)
{
    QWidget* widget = new QWidget(this);
    QVBoxLayout* layout = new QVBoxLayout(widget);
    layout->setMargin(0);

    label = new QLabel(widget);
    connect(label, SIGNAL(linkActivated(QString)), this, SLOT(linkActivated(QString)));

    QFile cssFile(":/resources/style.css");
    cssFile.open(QIODevice::ReadOnly);
    label->setStyleSheet(cssFile.readAll());

    layout->addWidget(label);
    addTab(widget, QString());
    tabBar()->hide();

    connect(qApp, SIGNAL(settingsChanged()), this, SLOT(updateContent()));
    connect(qApp, SIGNAL(connectionsChanged()), this, SLOT(updateContent()));
    updateContent();
}

void HomeView::showEvent(QShowEvent* event)
{
    QWidget::showEvent(event);
    window()->setWindowFilePath(tr("Home"));
}

void HomeView::linkActivated(const QString& link)
{
    QUrl url(link);
    if (url.scheme() == "about")
    {
        if (url.path() == "qt")
            Application::aboutQt();
        else if (url.path() == "oxygen")
            Application::aboutOxygen();
        else if (url.path() == "application")
            Application::aboutApplication();
    }
    else if (url.scheme() == "connect")
    {
        if (url.path().isEmpty())
        {
            emit connectTo(Connection());
        }
        else
        {
            int index = url.path().toInt();
            emit connectToImpl(Application::connections().at(index));
        }
    }
    else if (url.scheme() == "remove")
    {
        int index = url.path().toInt();
        Connections connections = Application::connections();
        connections.removeAt(index);
        Application::setConnections(connections);
    }
    else if (url.scheme() == "settings")
    {
        Application::showSettings();
    }
    else
    {
        QDesktopServices::openUrl(url);
    }
}

void HomeView::updateContent()
{
    QFile htmlFile(":/resources/index.html");
    htmlFile.open(QIODevice::ReadOnly);
    QString html = htmlFile.readAll();
    html = html.arg(Application::applicationName())
               .arg(Application::applicationSlogan())
               .arg(connectionList());
    label->setText(html);
}

QString HomeView::connectionList() const
{
    Connections connections = Application::connections();
    if (connections.isEmpty())
        return QString("<li>%1</li>").arg(tr("None"));

    QString html;
    for (int i = 0; i < connections.count(); ++i)
    {
        Connection connection = connections.at(i);
        html += QString("<li><a href='connect:%1'>%2</a> (%3:%4)&nbsp;<small>(<a href='remove:%1'>remove</a>)</small></li>")
            .arg(i).arg(connection.name).arg(connection.host).arg(connection.port);
    }
    return html;
}
